package com.mt.roll.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.SysPost;
import com.mt.roll.core.dal.entity.SysUserPost;
import com.mt.roll.core.dal.service.SysPostServiceBiz;
import com.mt.roll.core.dal.service.SysUserPostServiceBiz;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.TreeUtil;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.upms.params.SysPostParam;
import com.mt.roll.upms.service.PostService;
import com.mt.roll.upms.vo.SysPostVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * 合作伙伴岗位接口实现层
 */
@Service
public class PostServiceImpl extends BaseService implements PostService {
    @Autowired
    private SysPostServiceBiz sysPostServiceBiz;
    @Autowired
    private SysUserPostServiceBiz sysUserPostServiceBiz;

    @Override
    public R<SysPostVo> get(IdParam idParam) {
        SysPostVo postVo = SysPostVo.builder().build();
        SysPost sysPost = sysPostServiceBiz.getById(idParam.getId());
        BeanUtils.copyProperties(sysPost, postVo);
        return successful(postVo);
    }

    @Override
    public R<List<SysPostVo>> tree() {
        List<SysPost> sysPosts = sysPostServiceBiz.list();
        List<SysPostVo> sysPostVos = com.mt.roll.core.utils.BeanUtils.copyList(sysPosts, SysPostVo.class);
        return successful(TreeUtil.toTree(sysPostVos, 0L));
    }

    @Override
    public R<IdVo> add(SysPostParam sysPostParam) {
        //判断是否存在parentID为0 的根
        if (sysPostParam.getParentId() == 0 && sysPostServiceBiz.getOne(new QueryWrapper<SysPost>().eq(SysPost.PARENT_ID, sysPostParam.getParentId()).last("LIMIT 1")) != null) {
            return R.failed("已存在根目录，请选择一个根目录");
        }
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostParam, sysPost);
        setCommonField(sysPost);
        sysPostServiceBiz.save(sysPost);
        return successful(IdVo.builder().id(sysPost.getId()).build());
    }

    @Override
    public R<IdVo> update(SysPostParam sysPostParam) {
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostParam, sysPost);
        setUpdateCommonField(sysPost);
        sysPostServiceBiz.updateById(sysPost);
        return successful(IdVo.builder().id(sysPost.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        SysPost sysPost = sysPostServiceBiz.getById(enabledParam.getId());
        if (sysPost == null) {
            return failed("岗位不存在");
        }
        if (!enabledParam.getEnabled().equals(sysPost.getEnabled())) {
            SysPost updateSysPost = new SysPost().setId(sysPost.getId());
            updateSysPost.setEnabled(enabledParam.getEnabled());
            setUpdateCommonField(updateSysPost);
            sysPostServiceBiz.updateById(updateSysPost);
        }
        return successful(IdVo.builder().id(sysPost.getId()).build());
    }


    @Override
    public R<IdVo> delete(IdParam idParam) {
        //判断是否已有用户
        if (sysUserPostServiceBiz.count(new QueryWrapper<SysUserPost>().eq(SysUserPost.POST_ID, idParam.getId())) > 0) {
            return R.failed("岗位下存在用户，请先删除合作伙伴用户信息");
        }
        //判断是否存在下级
        SysPost sysPost = sysPostServiceBiz.getOne(new QueryWrapper<SysPost>().eq(SysPost.PARENT_ID, idParam.getId()).last("LIMIT 1"));
        if (sysPost != null) {
            return R.failed("存在下级岗位，请先删除下级岗位");
        }
        sysPostServiceBiz.removeById(idParam.getId());

        return successful(IdVo.builder().id(idParam.getId()).build());
    }
}
